package com.talent.study.netty;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;

/**
 * @Author: hszhou
 * @Date: 2025/4/25 16:08
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Slf4j
public class ServerHandler extends ChannelInboundHandlerAdapter {

    private int count;
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("客户端连接成功");
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
       log.info("客户端断开连接");
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        ByteBuf byteBuf = (ByteBuf)msg;

        byte[] bytes = new byte[byteBuf.readableBytes()];
        String s = new String(bytes, StandardCharsets.UTF_8);
        count++;
        log.info("收到消息[{}],次数【{}】",s,count);

        super.channelRead(ctx, msg);
    }


}
